package com.jf.apply_car.util;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@ControllerAdvice
@ResponseBody
public class AppExceptionHandler {

    @ExceptionHandler(IOException.class)
    public ResultJson handlerIOException(IOException e) {
        return ResultJson.error(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public ResultJson handlerBusinessException(BusinessException e) {
        return ResultJson.error(e.getCode(), e.getMessage());
    }

    /*监听runtime异常*/
    @ExceptionHandler(RuntimeException.class)
    public ResultJson handlerRuntimeException(RuntimeException e) {
        return ResultJson.error(e.getMessage());
    }

    /*
     * Exception是所有异常的父级
     * Exception的异常监听一定写在最后面
     * */
    @ExceptionHandler(Exception.class)
    public ResultJson handlerException(Exception e) {
        return ResultJson.error(e.getMessage());
    }

}
